package com.gg.fruit.controlller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.category.service.CategoryService;
import com.gg.entity.Category;
import com.gg.entity.Fruit;
import com.gg.fruit.service.FruitService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import helpers.baseservlet.BaseServlet;

@WebServlet("/fruit/LoadIndexServlet")
public class LoadIndexServlet extends BaseServlet {
	@Autowired
	private FruitService fruitService;
	@Autowired
	private CategoryService categoryService;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String page=request.getParameter("page");
		Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
		PageHelper.startPage(pageNum, 5);
		List<Fruit> fruits = fruitService.showAllHotFruits();
		PageInfo<Fruit> info = new PageInfo<Fruit>(fruits);
		if (pageNum > info.getPages()) {
			PageHelper.startPage(1, 5);
			fruits = fruitService.showAllHotFruits();
			info = new PageInfo<Fruit>(fruits);

		}
		List<Category> categorys = categoryService.LondAllCategorys();
		request.getSession().setAttribute("categorys", categorys);

		request.getSession().setAttribute("hotinfo", info);
		response.sendRedirect("/fruits/"+page+".jsp");
	}

}
